﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AB.ServiceDirectory.BO;
using AB.ServiceDirectory.Entities;

namespace AB.ServiceDirectory.Design.Org
{
    public partial class HAddSupportingMaterial : System.Web.UI.Page
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                BindDropDownListType();
                BindFields();
            }
        }

        /// <summary>
        /// Buttons the save click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonSaveClick(object sender, EventArgs e)
        {
            bool result=false;
            var supportingMaterial = GetSupportingMaterial();
            if (supportingMaterial!=null)
            {
                result = HSupportingMaterialBO.InsertSupportingMaterial(supportingMaterial);
            }

            if(result)
            {
                Response.Redirect("~/Organisation/HSupportingMaterials.aspx");
            }

        }

        /// <summary>
        /// Binds the type of the drop down list.
        /// </summary>
        private void BindDropDownListType()
        {
            DropDownListType.DataSource = HSupportingMaterialBO.GetAllSupportingMaterialType();
            DropDownListType.DataBind();
        }

        /// <summary>
        /// Binds the fields.
        /// </summary>
        private void BindFields()
        {
            TextBoxAddedBy.Text = HttpContext.Current.User.Identity.Name;
            TextBoxAddedDate.Text = DateTime.Now.ToShortDateString();
        }

        /// <summary>
        /// Gets the supporting material from fields in the Page
        /// </summary>
        private SupportingMaterial GetSupportingMaterial()
        {
            if (Session["userId"] != null)
            {
                var supportingMaterial = new SupportingMaterial();
                supportingMaterial.Url = string.IsNullOrEmpty(TextBoxURL.Text) ? string.Empty : TextBoxURL.Text;
                supportingMaterial.Description = string.IsNullOrEmpty(TextBoxDescription.Text)
                                                     ? string.Empty
                                                     : TextBoxDescription.Text;
                supportingMaterial.Type = string.IsNullOrEmpty(DropDownListType.SelectedValue)
                                              ? string.Empty
                                              : DropDownListType.SelectedValue;
                supportingMaterial.UserId = (int) Session["userId"];
                supportingMaterial.AddedDate = DateTime.Now;
                supportingMaterial.IsActive = true;
                supportingMaterial.OrganisationId = 1; //organisation name=Celeste662
                return supportingMaterial;
            }
            return null;
        }
    }
}